package com.it.base.arithmetic.linkend;

/**
 * 单向链表
 */
public class SingleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode hero1 =  new HeroNode(1,"宋江","及时雨");
        HeroNode hero2 =  new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode hero3 =  new HeroNode(3,"吴用","智多星");
        HeroNode hero4 =  new HeroNode(4,"林冲","豹子头");
        HeroNode hero5 =  new HeroNode(5,"张三","自定义");
        //创建链表
        SingLinkedList singLinkedList = new SingLinkedList();
      /*  singLinkedList.addNode(hero1);
        singLinkedList.addNode(hero2);
        singLinkedList.addNode(hero3);
        singLinkedList.addNode(hero4);
        singLinkedList.addNode(hero5);*/

        System.out.println("通过编号顺序来添加");
        singLinkedList.addNodeByOrder(hero1);
        singLinkedList.addNodeByOrder(hero3);
        singLinkedList.addNodeByOrder(hero5);
        singLinkedList.addNodeByOrder(hero4);
        singLinkedList.addNodeByOrder(hero2);
        HeroNode updatehero6 =  new HeroNode(2,"李四","玉麒麟·····");

        singLinkedList.update(updatehero6);

        singLinkedList.showList();
        System.out.println("测试删除1节点");
        singLinkedList.delHearNode(1);
        singLinkedList.delHearNode(5);
        singLinkedList.showList();
    }
}
//定义一个SingLinkedList来管理所有node
class SingLinkedList{
    private HeroNode head=new HeroNode(0,"","");

//添加节点到单向链表,不排序
    public void addNode(HeroNode heroNode){
        HeroNode temp = head;
        //遍历链表
        while (true){
            //找到链表的最后一个
            if(temp.next == null){
                break;
            }
            //如果没有找到，就后移
            temp=temp.next;

        }
        //退出循环，temp指向链表的最后一个节点,只需要把最后一个节点next指向新节点即可添加到链表的最后
        temp.next=heroNode;
    }
    //按排序的方式来添加节点
    public void addNodeByOrder(HeroNode heroNode){

        //头节点不能动，需要一个变量来帮助添加
        //因为是单向链表，需要找到要加入节点的前一个节点
        HeroNode temp = head;
        //标识符，添加的编号是否存在
        boolean flag = false;
        while (true){
            //说明已经找到最后节点了，那么直接添加到节点的最后即可
            if(temp.next==null){
                break;
            }
            //位置找到了，句在temp后面添加
            if(temp.next.no>heroNode.no){
                break;
            }
            //编号相同
            else if(temp.next.no ==heroNode.no){
                flag=true;
                break;
            }
            //指针后移
            temp = temp.next;
        }
        if(flag){
            System.out.println("编号存在，不能添加");


        }else {

            heroNode.next=temp.next;
            temp.next=heroNode;


        }

    }
    //根据编号no来修改，no编号不能改变
    public void update(HeroNode heroNode){
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        boolean flag = false;
        while (true){
                    if(temp==null){

                        break;
                    }
                    if(temp.no== heroNode.no){
                        flag=true;

                        break;
                    }
                    temp = temp.next;
        }
        if(flag){
            temp.name=heroNode.name;
            temp.nickname=heroNode.nickname;
        }
        else{
            System.out.println("没有找到no对应的节点");
        }
    }
    //根据编号来删除
    public void delHearNode(int no){
        HeroNode temp = head;
        boolean flag = false;
        while (true){
            if(temp.next==null){
                break;
            }
            //找到要删除节点的前一个节点
            if(temp.next.no ==no){
                flag=true;
                break;
            }
            temp = temp.next;

        }
        if(flag){
            temp.next=temp.next.next;
        }else {
            System.out.println("无法删除，没有找到要删除的节点");
        }
    }
    //显示链表
    public void showList(){
        //判断链表是否为空
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }
        //遍历链表
        HeroNode temp = head.next;
        while (true){
            //找到链表的最后一个
            if(temp == null){
                break;
            }
            System.out.println(temp);
            //一定要后移，不然死循环
            temp=temp.next;

        }

    }

}
//定义HeroNode,每一个HeroNode对象就是一个节点
class HeroNode{
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;
    public HeroNode(int no,String name,String nickname){
        this.no=no;
        this.name=name;
        this.nickname=nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
